package com.ruoyi.app.service.impl

import cn.hutool.core.bean.BeanUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.ruoyi.app.common.Data
import com.ruoyi.app.common.Fail
import com.ruoyi.app.common.Ok
import com.ruoyi.app.common.spider
import com.ruoyi.app.domain.dto.AdvertisingSettingDto
import com.ruoyi.app.domain.vo.*
import com.ruoyi.app.service.IndexApiService
import com.ruoyi.back.domain.*
import com.ruoyi.back.domain.Function
import com.ruoyi.back.enums.AdvertisingTypeEnum
import com.ruoyi.back.service.*
import com.ruoyi.back.service.impl.AdvertisingSettingService
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.io.InputStreamReader
import java.util.regex.Pattern

@Service
class IndexApiServiceImpl : IndexApiService {

    @Autowired
    private lateinit var advertisingService: OpenAdvertisingService

    @Autowired
    private lateinit var spreadCenterSettingService: SpreadCenterSettingService

    @Autowired
    private lateinit var wxPaySettingService: WxPaySettingService

    @Autowired
    private lateinit var aliPaySettingService: AliPaySettingService

    @Autowired
    private lateinit var functionService: FunctionService

    @Autowired
    private lateinit var advertisingSettingService: AdvertisingSettingService


    private val log: Logger = LoggerFactory.getLogger(IndexApiServiceImpl::class.java)

    override fun getIndexBannerList(): Data<List<AdvertisingVo>> =
        advertisingService.list(
            KtQueryWrapper(OpenAdvertising::class.java)
                .eq(OpenAdvertising::advertisingType, AdvertisingTypeEnum.BANNER.code)
                .eq(OpenAdvertising::isShow, true)
                .orderByDesc(OpenAdvertising::createTime)
        )
            .takeIf { it.isNotEmpty() }
            ?.run {
                Ok("获取轮播图数据成功", map {
                    AdvertisingVo().apply { BeanUtil.copyProperties(it, this) }
                })
            } ?: Ok("暂无数据", null)

    override fun getFunctionList(): Data<IndexFunction> =
        functionService.list(KtQueryWrapper(Function::class.java)
            .eq(Function::isShow, true)
            .orderByAsc(Function::orderNum))
            .takeIf { it.isNotEmpty() }
            ?.let {
                Ok("获取功能列表成功", kotlin.run {
                    val functionName = it.map { it.name }
                    val firstList = mutableListOf<FunctionVo>()
                    if (functionName.contains("提取短视频")) {
                        it.removeIf { it.name == "提取短视频" }
                        firstList.add(FunctionVo(name = "提取短视频", orderNum = 1 ))
                    }
                    if (functionName.contains("视频去水印")) {
                        it.removeIf { it.name == "视频去水印" }
                        firstList.add(FunctionVo(name = "视频去水印", orderNum = 2 ))
                    }
                    if (functionName.contains("视频加水印")) {
                        it.removeIf { it.name == "视频加水印" }
                        firstList.add(FunctionVo(name = "视频加水印", orderNum = 3 ))
                    }

                    IndexFunction().apply {
                        first = firstList
                        second = it.map {
                            FunctionVo().apply {
                                BeanUtil.copyProperties(it, this)
                            }
                        }
                    }
                })
            } ?: Ok("暂无数据", null)

    override fun getGlobalSetting(): Data<GlobalSetting> =
        Ok("获取全局配置参数成功", GlobalSetting().apply {
            spreadCenterSettingService.getOne(
                KtQueryWrapper(SpreadCenterSetting::class.java)
            )?.let {
                BeanUtil.copyProperties(it, this)
            }

            wxPaySettingService.getOne(KtQueryWrapper(WxPaySetting::class.java))
                ?.let {
                    wxPayIcon = it.isUse ?: false
                }

            aliPaySettingService.getOne(KtQueryWrapper(AliPaySetting::class.java))
                ?.let {
                    aliPayIcon = it.isUse ?: false
                }
        })

    override fun getAdvertisingSetting(advertisingSettingDto: AdvertisingSettingDto): Data<AdvertisingSettingVo> =
        Ok("获取广告配置参数成功", advertisingSettingService.getOne(KtQueryWrapper(AdvertisingSetting::class.java)
            .eq(AdvertisingSetting::advertisingType, advertisingSettingDto.advertisingType))
            ?.let {
                AdvertisingSettingVo().apply {
                    BeanUtil.copyProperties(it, this)
                }
            } ?: AdvertisingSettingVo())
    override fun resolveVideo(urlText: String): Data<List<VideoVo>> =
        urlText.urls().takeIf { it.isNotEmpty() }
            ?.run {
                Ok("提取短视频成功", mapNotNull {
                    it.spider()
                })
            } ?: Fail("暂时没有提取出来相关链接!")


    fun String.api() = "https://tenapi.cn/video/?url=$this"
    fun String.urls(): List<String> =
        Pattern.compile("(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]")
            .let { pattern ->
                val urls = arrayListOf<String>()
                pattern.matcher(this).let { matcher ->
                    while (matcher.find()) {
                        urls.add(matcher.group())
                    }
                }
                urls
            }
}

fun main() {
    ProcessBuilder("").run {
        val reader = InputStreamReader(start().inputStream)
        val buffer = charArrayOf()
        reader.read(buffer)
        println(buffer)
    }
}
